package com.pray.praymall.product.web;

import com.pray.praymall.product.entity.CategoryEntity;
import com.pray.praymall.product.service.CategoryService;
import com.pray.praymall.product.vo.Catalog2Vo;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * do something
 *
 * @author 魂悸魄
 * @date 2020/9/14 10:18
 */
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redisson;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        // TODO 查出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        // 视图解析器进行拼串
        // classpath:templates/ + 返回值 + .html
        model.addAttribute("categorys", categoryEntities);
        return "index";

    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        return categoryService.getCatalogJson();
    }


    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        // 1、获取一把锁，只要锁的名字一样，就是同一把锁
        RLock lock = redisson.getLock("myLock");
        lock.lock();
        try {
            System.out.println("加锁成功，执行业务......" + Thread.currentThread().getName());
            Thread.sleep(30000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("释放锁：" + Thread.currentThread().getName());
            lock.unlock();
        }
        return "hello";
    }

    @ResponseBody
    @GetMapping("lockDoor")
    public String lockDoor() throws InterruptedException {
        RCountDownLatch lockDoor = redisson.getCountDownLatch("lockDoor");
        lockDoor.trySetCount(5);
        lockDoor.await();
        return "锁门，放假了。。。";
    }

    @ResponseBody
    @GetMapping("/gogogo/{id}")
    public String gogogo(@PathVariable("id") Long id) {
        RCountDownLatch lockDoor = redisson.getCountDownLatch("lockDoor");
        lockDoor.countDown();
        return id + "班的人走了";
    }

    /**
     * 车库停车
     * @return
     */
    @ResponseBody
    @GetMapping("/park")
    public String park() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        park.trySetPermits(3);
        // 获取一个信号，获取一个值，占一个车位
        park.acquire();
        return "ok";
    }

    /**
     * 车库停车
     * @return
     */
    @ResponseBody
    @GetMapping("/go")
    public String go() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        // 释放一个信号，释放一个车位
        park.release();
        return "ok";
    }
}
